/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ControlManager;
import java.awt.Color;

import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;


import model.*;

/**
 *
 *
 */
public class MainWindow extends javax.swing.JFrame {

    public static ControlManager man = new ControlManager();
    public static javax.swing.JButton bot = null;
    private static int press = 0;
    private static int antX = 0;
    private static int antY = 0;
    public static BufferedImage selImg = null;
    private model.Object dragObject = null;
   // public static Canvas canvas =new Canvas();
    /**
     * Creates new form MainWindow
     */
    public MainWindow() {
        initComponents();


        canvas.setFocusable(true);
        canvas.requestFocusInWindow();
        this.setTitle("Editor de Mapa");
        btnCasa1.setToolTipText("Casa");
        btnCasa2.setToolTipText("Casa");
        btnCamino1.setToolTipText("Camino");
        btnCamino2.setToolTipText("Camino");
        btnCamino3.setToolTipText("Camino");
        btnCamino4.setToolTipText("Camino");
        btnCamino5.setToolTipText("Camino");
        btnCamino6.setToolTipText("Camino");
        btnCamino7.setToolTipText("Camino");
        btnCamino8.setToolTipText("Camino");
        btnCamino9.setToolTipText("Camino");
        btnArbol1.setToolTipText(("Arbol"));
        btnArbol2.setToolTipText(("Arbol"));
        btnArbol3.setToolTipText(("Arbol"));

    }

        public MainWindow(Map map) {
    

        
        man.setMap(map);
        man.save("DB.xml");
        man.loadMap("DB.xml");
        initComponents();    
        canvas.setFocusable(true);
        canvas.requestFocusInWindow();
        this.setTitle("Editor de Mapa");
        btnCasa1.setToolTipText("Casa");
        btnCasa2.setToolTipText("Casa");
        btnCamino1.setToolTipText("Camino");
        btnCamino2.setToolTipText("Camino");
        btnCamino3.setToolTipText("Camino");
        btnCamino4.setToolTipText("Camino");
        btnCamino5.setToolTipText("Camino");
        btnCamino6.setToolTipText("Camino");
        btnCamino7.setToolTipText("Camino");
        btnCamino8.setToolTipText("Camino");
        btnCamino9.setToolTipText("Camino");
        btnArbol1.setToolTipText(("Arbol"));
        btnArbol2.setToolTipText(("Arbol"));
        btnArbol3.setToolTipText(("Arbol"));

    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnDel = new javax.swing.JButton();
        btnCht = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        coordX = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        coordY = new javax.swing.JLabel();
        btnCamino7 = new javax.swing.JButton();
        btnCasa2 = new javax.swing.JButton();
        btnCasa1 = new javax.swing.JButton();
        btnCamino9 = new javax.swing.JButton();
        btnCamino8 = new javax.swing.JButton();
        btnCamino1 = new javax.swing.JButton();
        btnCamino3 = new javax.swing.JButton();
        btnCamino2 = new javax.swing.JButton();
        btnCamino4 = new javax.swing.JButton();
        btnCamino6 = new javax.swing.JButton();
        btnCamino5 = new javax.swing.JButton();
        btnArbol1 = new javax.swing.JButton();
        btnArbol3 = new javax.swing.JButton();
        btnArbol2 = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        btnNew = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnLoad = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        canvas = new view.Canvas();
        mMap = new view.MiniMap();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Map Editor");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnDel.setText("Eliminar");
        btnDel.setName("btnEliminar"); // NOI18N
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });
        getContentPane().add(btnDel, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 90, 123, -1));

        btnCht.setText("Modificar Terreno");
        btnCht.setName("btnModificarTerreno"); // NOI18N
        btnCht.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChtActionPerformed(evt);
            }
        });
        getContentPane().add(btnCht, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 60, 123, -1));

        jLabel1.setText("X:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 450, -1, 30));

        coordX.setText("-");
        getContentPane().add(coordX, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 450, -1, 30));

        jLabel3.setText("Y:");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 470, -1, 30));

        coordY.setText("-");
        getContentPane().add(coordY, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 470, -1, 30));

        btnCamino7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/path_sew.png"))); // NOI18N
        btnCamino7.setToolTipText("");
        btnCamino7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCamino7ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCamino7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 310, 60, 50));

        btnCasa2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/house1.png"))); // NOI18N
        btnCasa2.setToolTipText("");
        btnCasa2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCasa2ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCasa2, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 130, 50, 50));

        btnCasa1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/house2.png"))); // NOI18N
        btnCasa1.setToolTipText("");
        btnCasa1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCasa1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCasa1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, 60, 50));

        btnCamino9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/road_nsew.png"))); // NOI18N
        btnCamino9.setToolTipText("");
        btnCamino9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCamino9ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCamino9, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 310, 60, 50));

        btnCamino8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/path_nse.png"))); // NOI18N
        btnCamino8.setToolTipText("");
        btnCamino8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCamino8ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCamino8, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 310, 50, 50));

        btnCamino1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/path_ew.png"))); // NOI18N
        btnCamino1.setToolTipText("");
        btnCamino1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCamino1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCamino1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 190, 60, 50));

        btnCamino3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/path_sw.png"))); // NOI18N
        btnCamino3.setToolTipText("");
        btnCamino3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCamino3ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCamino3, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 190, 60, 50));

        btnCamino2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/path_ns.png"))); // NOI18N
        btnCamino2.setToolTipText("");
        btnCamino2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCamino2ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCamino2, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 190, 50, 50));

        btnCamino4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/path_ne.png"))); // NOI18N
        btnCamino4.setToolTipText("");
        btnCamino4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCamino4ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCamino4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 250, 60, 50));

        btnCamino6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/road_se.png"))); // NOI18N
        btnCamino6.setToolTipText("");
        btnCamino6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCamino6ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCamino6, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 250, 60, 50));

        btnCamino5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/path_nw.png"))); // NOI18N
        btnCamino5.setToolTipText("");
        btnCamino5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCamino5ActionPerformed(evt);
            }
        });
        getContentPane().add(btnCamino5, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 250, 50, 50));

        btnArbol1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/trees13.png"))); // NOI18N
        btnArbol1.setToolTipText("");
        btnArbol1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnArbol1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnArbol1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 380, 60, 50));

        btnArbol3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/trees12.png"))); // NOI18N
        btnArbol3.setToolTipText("");
        btnArbol3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnArbol3ActionPerformed(evt);
            }
        });
        getContentPane().add(btnArbol3, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 380, 60, 50));

        btnArbol2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/trees18.png"))); // NOI18N
        btnArbol2.setToolTipText("");
        btnArbol2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnArbol2ActionPerformed(evt);
            }
        });
        getContentPane().add(btnArbol2, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 380, 50, 50));

        jToolBar1.setRollover(true);

        btnNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/new32.png"))); // NOI18N
        btnNew.setToolTipText("");
        btnNew.setFocusable(false);
        btnNew.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNew.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });
        jToolBar1.add(btnNew);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/save32_1.png"))); // NOI18N
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSave);

        btnLoad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/open32.png"))); // NOI18N
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });
        jToolBar1.add(btnLoad);

        getContentPane().add(jToolBar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 590, 40));

        jButton1.setText("jButton1");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 100, 0, 0));

        jToggleButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pointer-icon.png"))); // NOI18N
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jToggleButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 130, 60, 50));

        canvas.setBackground(new java.awt.Color(0, 0, 0));
        canvas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                canvasMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                canvasMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                canvasMouseReleased(evt);
            }
        });
        canvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                canvasMouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                canvasMouseMoved(evt);
            }
        });
        canvas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                canvasKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout canvasLayout = new javax.swing.GroupLayout(canvas);
        canvas.setLayout(canvasLayout);
        canvasLayout.setHorizontalGroup(
            canvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 510, Short.MAX_VALUE)
        );
        canvasLayout.setVerticalGroup(
            canvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 370, Short.MAX_VALUE)
        );

        getContentPane().add(canvas, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 50, 510, 370));

        mMap.setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout mMapLayout = new javax.swing.GroupLayout(mMap);
        mMap.setLayout(mMapLayout);
        mMapLayout.setHorizontalGroup(
            mMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );
        mMapLayout.setVerticalGroup(
            mMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        getContentPane().add(mMap, new org.netbeans.lib.awtextra.AbsoluteConstraints(770, 50, 200, 200));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
        // TODO add your handling code here:
        //   	DeleteFrame myDel = new DeleteFrame();
        //	myDel.setVisible(true);

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DeleteFrm().setVisible(true);
            }
        });
    }//GEN-LAST:event_btnDelActionPerformed

    private void btnChtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChtActionPerformed
        // TODO add your handling code here:
        //   ChangeTerrainFrame myTerrain = new ChangeTerrainFrame();
//		myTerrain.setVisible(true);

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChangeTerrainFrm().setVisible(true);
            }
        });
    }//GEN-LAST:event_btnChtActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        //man.save("DB.xml");
    }//GEN-LAST:event_formWindowClosing
    private BufferedImage imgFromButton() {
        BufferedImage img = null;
        if (bot != null) {
            if (bot == btnCasa1) {
                img = canvas.getImage("house2");
            } else if (bot == btnCasa2) {
                img = canvas.getImage("house1");
            } else if (bot == btnCamino1) {
                img = canvas.getImage("path_ew");
            } else if (bot == btnCamino2) {
                img = canvas.getImage("path_ns");
            } else if (bot == btnCamino3) {
                img = canvas.getImage("path_sw");
            } else if (bot == btnCamino4) {
                img = canvas.getImage("path_ne");
            } else if (bot == btnCamino5) {
                img = canvas.getImage("path_nw");
            } else if (bot == btnCamino6) {
                img = canvas.getImage("road_se");
            } else if (bot == btnCamino7) {
                img = canvas.getImage("path_sew");
            } else if (bot == btnCamino8) {
                img = canvas.getImage("path_nse");
            } else if (bot == btnCamino9) {
                img = canvas.getImage("road_nsew");
            } else if (bot == btnArbol1) {
                img = canvas.getImage("trees13");
            } else if (bot == btnArbol2) {
                img = canvas.getImage("trees18");
            } else if (bot == btnArbol3) {
                img = canvas.getImage("trees12");
            }
        }
        return img;
    }

    private void canvasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvasMouseClicked
        // TODO add your handling code here:

        int x = evt.getX();
        int y = evt.getY();

        if (canvas.perteneceMapa(x, y)) {
            canvas.repintar(antX, antY);
            canvas.getCellPos(x, y);
            int a = Integer.parseInt(coordX.getText());
            int b = Integer.parseInt(coordY.getText());
            canvas.xHigh = a;
            canvas.yHigh = b;
            /*Cell c=man.getMap().getCell(Integer.parseInt(coordX.getText())-1,Integer.parseInt(coordY.getText())-1);
             c.setType(1);
             canvas.drawCellRepaint(c, 0, 0);*/
            //getContentPane().validate();
            //getContentPane().repaint();
            canvas.repintar(a, b);
            antX = a;
            antY = b;
        } else {

            coordX.setText("");
            coordY.setText("");
        }

        //coordX.setText(Integer.toString(x));
        //coordY.setText(Integer.toString(y));
        if (bot != null && canvas.perteneceMapa(x, y)) {
            Inanimate oNuevo = new Inanimate();
            int a = Integer.parseInt(MainWindow.coordX.getText());
            int b = Integer.parseInt(MainWindow.coordY.getText());
            a--;
            b--;
            Cell c = man.getMap().getCell(a, b);
            if (bot == btnCasa1) {
                oNuevo.setName("Casa1");
            } else if (bot == btnCasa2) {
                oNuevo.setName("Casa2");
            } else if (bot == btnCamino1) {
                oNuevo.setName("Camino1");
            } else if (bot == btnCamino2) {
                oNuevo.setName("Camino2");
            } else if (bot == btnCamino3) {
                oNuevo.setName("Camino3");
            } else if (bot == btnCamino4) {
                oNuevo.setName("Camino4");
            } else if (bot == btnCamino5) {
                oNuevo.setName("Camino5");
            } else if (bot == btnCamino6) {
                oNuevo.setName("Camino6");
            } else if (bot == btnCamino7) {
                oNuevo.setName("Camino7");
            } else if (bot == btnCamino8) {
                oNuevo.setName("Camino8");
            } else if (bot == btnCamino9) {
                oNuevo.setName("Camino9");
            } else if (bot == btnArbol1) {
                oNuevo.setName("Arbol1");
            } else if (bot == btnArbol2) {
                oNuevo.setName("Arbol2");
            } else if (bot == btnArbol3) {
                oNuevo.setName("Arbol3");
            }

            c.setObject(oNuevo);
            canvas.drawCell(c, a, b);
            canvas.repintar(a, b);
            MainWindow.man.save("DB.xml");
            System.out.println("linea");
         
        }


    }//GEN-LAST:event_canvasMouseClicked

    private void btnCasa1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCasa1ActionPerformed

        bot = btnCasa1;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCasa1ActionPerformed

    private void btnCasa2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCasa2ActionPerformed
        bot = btnCasa2;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCasa2ActionPerformed

    private void btnCamino1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCamino1ActionPerformed
        bot = btnCamino1;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCamino1ActionPerformed

    private void btnCamino3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCamino3ActionPerformed
        bot = btnCamino3;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCamino3ActionPerformed

    private void btnCamino2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCamino2ActionPerformed
        bot = btnCamino2;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCamino2ActionPerformed

    private void btnCamino4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCamino4ActionPerformed
        bot = btnCamino4;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCamino4ActionPerformed

    private void btnCamino5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCamino5ActionPerformed
        bot = btnCamino5;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCamino5ActionPerformed

    private void btnCamino6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCamino6ActionPerformed
        bot = btnCamino6;
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCamino6ActionPerformed

    private void btnCamino7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCamino7ActionPerformed
        bot = btnCamino7;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCamino7ActionPerformed

    private void btnCamino8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCamino8ActionPerformed
        bot = btnCamino8;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCamino8ActionPerformed

    private void btnCamino9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCamino9ActionPerformed
        bot = btnCamino9;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnCamino9ActionPerformed

    private void btnArbol1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnArbol1ActionPerformed
        bot = btnArbol1;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnArbol1ActionPerformed

    private void btnArbol2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnArbol2ActionPerformed
        bot = btnArbol2;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnArbol2ActionPerformed

    private void btnArbol3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnArbol3ActionPerformed
        bot = btnArbol3;
        selImg = imgFromButton();
        jToggleButton1.setSelected(false);
    }//GEN-LAST:event_btnArbol3ActionPerformed

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        // TODO add your handling code here:
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewFrm().setVisible(true);
            }
        });
    }//GEN-LAST:event_btnNewActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new saveFrm().setVisible(true);
            }
        });
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed
        // TODO add your handling code here:
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new loadFrm().setVisible(true);
            }
        });
    }//GEN-LAST:event_btnLoadActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:
        if (jToggleButton1.isSelected()) {
            bot = null;
            selImg = null;
        }

    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void canvasMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvasMouseDragged
        // TODO add your handling code here:
        int x = evt.getX();
        int y = evt.getY();
        canvas.requestFocusInWindow();
        if (canvas.perteneceMapa(x, y)) {

            canvas.getCellPos(x, y);
            int a = Integer.parseInt(coordX.getText());
            int b = Integer.parseInt(coordY.getText());
            if (antX != a || antY != b) {
                canvas.repintar(antX, antY);
                antX = a;
                antY = b;
                canvas.xHigh = a;
                canvas.yHigh = b;
                canvas.repintar(a, b);

            }
        } else {
            coordX.setText("-");
            coordY.setText("-");
        }
    }//GEN-LAST:event_canvasMouseDragged

    private void canvasMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvasMousePressed
        // TODO add your handling code here:
        int x = evt.getX();
        int y = evt.getY();

        if (canvas.perteneceMapa(x, y)) {
            //canvas.repintar(antX, antY);
            canvas.getCellPos(x, y);
            int a = Integer.parseInt(coordX.getText());
            int b = Integer.parseInt(coordY.getText());
            canvas.xHigh = a;
            canvas.yHigh = b;
            antX = a;
            antY = b;
            Cell c = man.getMap().getCell(a - 1, b - 1);
            dragObject = c.getObject();
            if (bot == null)//si no se ha seleccionado ningun boton el selImg sirve para pintar el objeto mientras se mueve
            {
                selImg = canvas.getTile(c);
            }
            c.setObject(null);
            canvas.repintar(a, b);
        }


    }//GEN-LAST:event_canvasMousePressed

    private void canvasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvasMouseReleased
        // TODO add your handling code here:
        int x = evt.getX();
        int y = evt.getY();
        if (bot == null) {
            selImg = null;
        }
        if (canvas.perteneceMapa(x, y)) {
            canvas.repintar(antX, antY);
            canvas.getCellPos(x, y);
            int a = Integer.parseInt(coordX.getText());
            int b = Integer.parseInt(coordY.getText());
            canvas.xHigh = a;
            canvas.yHigh = b;
            /*Cell c=man.getMap().getCell(Integer.parseInt(coordX.getText())-1,Integer.parseInt(coordY.getText())-1);
             c.setType(1);
             canvas.drawCellRepaint(c, 0, 0);*/
            //getContentPane().validate();
            //getContentPane().repaint();
            canvas.repintar(a, b);
            antX = a;
            antY = b;
            Cell c = man.getMap().getCell(Integer.parseInt(coordX.getText()) - 1, Integer.parseInt(coordY.getText()) - 1);
            c.setObject(dragObject);
            dragObject = null;
        }
    }//GEN-LAST:event_canvasMouseReleased

    private void canvasMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvasMouseMoved
        // TODO add your handling code here:
        int x = evt.getX();
        int y = evt.getY();
        canvas.requestFocusInWindow();
        if (canvas.perteneceMapa(x, y)) {

            canvas.getCellPos(x, y);
            int a = Integer.parseInt(coordX.getText());
            int b = Integer.parseInt(coordY.getText());
            if (antX != a || antY != b) {
                canvas.repintar(antX, antY);
                antX = a;
                antY = b;
                canvas.xHigh = a;
                canvas.yHigh = b;
                canvas.repintar(a, b);

            }
        } else {
            coordX.setText("-");
            coordY.setText("-");
        }
    }//GEN-LAST:event_canvasMouseMoved

    private void canvasKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_canvasKeyPressed
        // TODO add your handling code here:

        System.out.println(canvas.secX+" "+canvas.secY + "DIm:" + canvas.secW + " " + canvas.secH);
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {//&& !coordX.getText().equals("") &&
            // !coordY.getText().equals("")){          
            int x = canvas.xHigh - 1;
            int y = canvas.yHigh - 1;

            Cell c = MainWindow.man.getMap().getCell(x, y);
            c.setObject(null);
            canvas.repintar(x, y);
        } else if (evt.getKeyCode() == KeyEvent.VK_UP) {
            canvas.secY -= canvas.step;
            canvas.repaint();
        } else if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            canvas.secY += canvas.step;
            canvas.repaint();
        } else if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
            canvas.secX -= canvas.step;
            canvas.repaint();
        } else if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
            canvas.secX += canvas.step;
            canvas.repaint();
        }
    }//GEN-LAST:event_canvasKeyPressed

    private void canvasKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_canvasKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_canvasKeyTyped

    private void canvasKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_canvasKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_canvasKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnArbol1;
    private javax.swing.JButton btnArbol2;
    private javax.swing.JButton btnArbol3;
    private javax.swing.JButton btnCamino1;
    private javax.swing.JButton btnCamino2;
    private javax.swing.JButton btnCamino3;
    private javax.swing.JButton btnCamino4;
    private javax.swing.JButton btnCamino5;
    private javax.swing.JButton btnCamino6;
    private javax.swing.JButton btnCamino7;
    private javax.swing.JButton btnCamino8;
    private javax.swing.JButton btnCamino9;
    private javax.swing.JButton btnCasa1;
    private javax.swing.JButton btnCasa2;
    private javax.swing.JButton btnCht;
    private javax.swing.JButton btnDel;
    private javax.swing.JButton btnLoad;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnSave;
    public static view.Canvas canvas;
    public static javax.swing.JLabel coordX;
    public static javax.swing.JLabel coordY;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToolBar jToolBar1;
    public static view.MiniMap mMap;
    // End of variables declaration//GEN-END:variables
}
